/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.language.python;

import org.apache.camel.Exchange;
import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.support.ExpressionSupport;
import org.python.core.PyCode;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class PythonExpression extends ExpressionSupport {

    private final String expressionString;
    private final Class<?> type;
    private final PythonInterpreter compiler;
    private final PyCode compiledExpression;

    public PythonExpression(String expressionString, Class<?> type) {
        this.expressionString = expressionString;
        this.type = type;
        this.compiler = new PythonInterpreter();
        try {
            this.compiledExpression = compiler.compile(expressionString);
        } catch (Exception e) {
            throw new ExpressionIllegalSyntaxException(expressionString, e);
        }
    }

    public static PythonExpression python(String expression) {
        return new PythonExpression(expression, Object.class);
    }

    @Override
    public <T> T evaluate(Exchange exchange, Class<T> type) {
        try {
            compiler.set("exchange", exchange);
            compiler.set("context", exchange.getContext());
            compiler.set("exchangeId", exchange.getExchangeId());
            compiler.set("message", exchange.getMessage());
            compiler.set("headers", exchange.getMessage().getHeaders());
            compiler.set("properties", exchange.getAllProperties());
            compiler.set("body", exchange.getMessage().getBody());

            PyObject out = compiler.eval(compiledExpression);
            if (out != null) {
                String value = out.toString();
                return exchange.getContext().getTypeConverter().convertTo(type, value);
            }
        } catch (Exception e) {
            throw new ExpressionIllegalSyntaxException(expressionString, e);
        } finally {
            compiler.cleanup();
        }
        return null;
    }

    public Class<?> getType() {
        return type;
    }

    @Override
    protected String assertionFailureMessage(Exchange exchange) {
        return expressionString;
    }

    @Override
    public String toString() {
        return "Python[" + expressionString + "]";
    }

}
